package com.ym.pms.utils;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.HashMap;

/**
 * @Author: xybh
 * @Description:
 * @Date: Created in 10:36 2020/12/4
 * @Modified:
 */

@Component
public class SmsUtil {

    private static String URL;
    private static String CLIENT_ID;
    private static String SECRET;

    @Value("${sms.verifyUri}")
    public void setURL(String URL) {
        SmsUtil.URL = URL;
    }

    @Value("${sms.clientId}")
    public void setClientId(String clientId) {
        CLIENT_ID = clientId;
    }

    @Value("${sms.secret}")
    public void setSECRET(String SECRET) {
        SmsUtil.SECRET = SECRET;
    }

    public static String sendMessage(String phone) {
        HashMap<String, String> map = new HashMap<>();
        map.put("recipient", phone);
        map.put("clientId", CLIENT_ID);
        map.put("clientSecret", SECRET);
        return HttpClientUtil.doPostJson(URL, JSON.toJSONString(map));
    }

    public static boolean verify(String phone, String ticket){
        String vt = (String) RedisUtils.get("sms-" + phone);
        return !StringUtils.isEmpty(vt) && ticket.equals(vt);
    }
}
